package com.warm.pump.module.work.service.impl;

import com.warm.pump.executor.InstanceService;
import com.warm.pump.frame.util.SpringBeanUtil;
import com.warm.pump.module.sys.bean.po.gen.DictItem;
import com.warm.pump.module.work.bean.WorkItemDto;
import com.warm.pump.module.work.bean.po.gen.Work;
import com.warm.pump.module.work.bean.po.gen.WorkItemSub;
import com.warm.pump.module.work.dao.gen.WorkBoDao;
import com.warm.pump.module.work.dao.gen.WorkDao;
import com.warm.pump.module.work.dao.gen.WorkItemSubDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.warm.pump.module.work.dao.gen.WorkItemDao;
import com.warm.pump.module.work.bean.po.gen.WorkItem;
import com.warm.pump.module.work.service.IWorkItemService;
import com.warm.pump.frame.gen.bean.PageBean;
import org.springframework.transaction.annotation.Transactional;


@Service("WorkItemService")
public class WorkItemServiceImpl implements IWorkItemService {
	@Autowired
	private WorkItemDao workItemDao;

	@Autowired
	private WorkItemSubDao workItemSubDao;

	@Autowired
	private WorkBoDao workBoDao;
	@Autowired
	private WorkDao workDao;
	@Autowired
	private InstanceService instanceService;

	@Value("${pump.executor.beanMode.basePackages}")
	private String basePackages;
	@Value("${pump.executor.etlMode.processBeansPackages}")
	private String processBeansPackages;

	private List<DictItem> serviceBeans;


	@Override
	public WorkItem queryObject(Long id) {
		return workItemDao.selectByPrimaryKey(id);
	}

	public WorkItem queryObject(Map<String, Object> map) {
		return workItemDao.selectByMap(map);
	}

	@Override
	public List<WorkItem> queryList(Map<String, Object> map) {
		return workItemDao.getListByMap(map);
	}


	@Override
	public PageBean getPageBeanByMap(Map map, int pageNo, int pageSize) {
		return workItemDao.getPageBeanByMap(map, pageNo, pageSize);
	}


	@Override
	public int queryTotal(Map<String, Object> map) {
		return workItemDao.countTotalByMap(map);
	}

	@Override
	@Transactional
	public void save(WorkItemDto workItemDto) {
		workItemDao.insertSelective(workItemDto);
		saveSub(workItemDto);
	}

	@Override
	public void update(WorkItem workItem) {
		workItemDao.updateByPrimaryKeySelective(workItem);
	}

	@Override
	public void update(WorkItemDto workItemDto) {
		workItemDao.updateByPrimaryKeySelective(workItemDto);

		Map<String,Object> condMap = new HashMap<>();
		condMap.put("itemId",workItemDto.getId());
		workItemSubDao.deleteByMap(condMap);
		saveSub(workItemDto);
	}

	@Override
	public void delete(Long id) {
		workItemDao.deleteByPrimaryKey(id);
	}

	@Override
	public void delete(Map<String, Object> map) {
		workItemDao.deleteByMap(map);
	}

	@Override
	public void deleteBatch(Long[] ids) {
		Map<String, Object> map = new HashMap<>();
		map.put("id_in", ids);
		workItemDao.deleteByMap(map);
	}

	/**
	 * 获取任务详表 排序值
	 *
	 * @return
	 */
	public Integer getNextExecNum(Long itemId) {
		Map<String, Object> condMap = new HashMap<>();
		condMap.put("itemId", itemId);
		return workBoDao.selectNextExecNum(condMap);
	}

	public List<DictItem> getServiceBeans() {
		if(null != serviceBeans&&serviceBeans.size()>0){
			return serviceBeans;
		}
		serviceBeans =  SpringBeanUtil.getServiceBeans(basePackages);
		return serviceBeans;
	}

	public List<DictItem> getProcessServiceBeans() {
		return SpringBeanUtil.getServiceBeans(processBeansPackages);
	}

	public List<DictItem> getMethodsByBeanName(String beanName) {
		return SpringBeanUtil.getMethodsByBeanName(beanName);
	}

	@Override
	public void run(Long[] ids) {
		for (Long id : ids) {
			this.createInstByWorkItem(id);
		}
	}


	/**
	 * 通过作业明细创建实例
	 *
	 * @param itemDtlId
	 */
	@Transactional
	public void createInstByWorkItem(Long itemDtlId) {
		WorkItem workItemDtl = workItemDao.selectByPrimaryKey(itemDtlId);
		Work work = workDao.selectByPrimaryKey(workItemDtl.getWorkId());
		//创建调度实例
		String instId = instanceService.createInst("workItem", workItemDtl.getWorkId(), workItemDtl.getItemName(), 1, "作业明细列表中立即执行");
		//创建调度实例明细
		instanceService.createInstDtl(instId, work.getId(), work.getWorkName(), workItemDtl.getId(), workItemDtl.getItemName(), null);

	}
	/**
	 * 保存 子SQL集
	 *
	 * @param workItemDtlDto
	 */
	private void saveSub(WorkItemDto workItemDtlDto) {
		List<WorkItemSub> workItemDtlSubs = workItemDtlDto.getWorkItemSubs();

		if (workItemDtlSubs == null || workItemDtlSubs.size() == 0) {
			return;
		}

		for (WorkItemSub dtlSub : workItemDtlSubs) {
			dtlSub.setId(null);
			dtlSub.setItemId(workItemDtlDto.getId());
			dtlSub.setCreateTime(new Date());
			dtlSub.setUpdateTime(new Date());
			workItemSubDao.insertSelective(dtlSub);
		}

	}
	
}
